package main

import (
	"fmt"
	"reflect"
)

/*
*
反射使用
ValueOf
TypeOf
*/
func reflectNum(arg interface{}) {
	fmt.Println("type of", reflect.TypeOf(arg),
		"value of", reflect.ValueOf(arg))
}

type User struct {
	Id   int
	Name string
	Age  int
}

func (user User) Test() string {
	return "ceshi"
}

func main() {
	//var num float64 = 1.2345
	//reflectNum(num)
	user := User{1, "abcd", 23}
	DoFiledAndMethod(user)
}

func DoFiledAndMethod(input interface{}) {
	inputType := reflect.TypeOf(input)
	inputValue := reflect.ValueOf(input)
	//拿到所有字段的类型和值
	for i := 0; i < inputType.NumField(); i++ {
		field := inputType.Field(i)
		value := inputValue.Field(i).Interface()
		fmt.Printf("%s: %v = %v\n", field.Name, field.Type, value)
	}
	fmt.Println("===========")
	//拿到所有的方法
	for i := 0; i < inputType.NumMethod(); i++ {
		m := inputType.Method(i)
		fmt.Printf("%s: %v\n", m.Name, m.Type)
	}

	fmt.Println(inputValue, inputType)

}
